package org.example.coffeeshoporderingsystem.common.ui;

import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.layout.AnchorPane;
import org.example.coffeeshoporderingsystem.common.constants.UIViewType;
import org.example.coffeeshoporderingsystem.login.controller.LoginController;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class UIManager implements Initializable {
    public AnchorPane mainPanel;
    public static ObservableList<Node> nodeObservableList;

    public static void switchToReport() throws IOException {
        nodeObservableList.clear();
        var fxmlLoader = new FXMLLoader(LoginController.class.getResource(UIViewType.REPORT.getViewName()));
        nodeObservableList.add(fxmlLoader.load()) ;
    }

    public static void showAlertError(String msg) {
        // 弹出警告框
        Alert alert = new Alert(Alert.AlertType.ERROR,msg);
        alert.show();
    }

    public static void showAlertInfo(String msg) {
        // 弹出警告框
        Alert alert = new Alert(Alert.AlertType.INFORMATION,msg);
        alert.show();
    }

    public static void showAlertWarn(String msg) {
        // 弹出警告框
        Alert alert = new Alert(Alert.AlertType.WARNING,msg);
        alert.show();
    }

    public static void showAlertConfirm(String msg) {
        // 弹出警告框
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION,msg);
        alert.show();
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        nodeObservableList = mainPanel.getChildren();
        try {
            switchToLogin();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    public static void switchToLogin() throws IOException {
        nodeObservableList.clear();
        var fxmlLoader = new FXMLLoader(UIManager.class.getResource(UIViewType.LOIGN.getViewName()));
        nodeObservableList.add(fxmlLoader.load()) ;
    }

    public static void switchToMain() throws IOException {
        nodeObservableList.clear();
        var fxmlLoader = new FXMLLoader(UIManager.class.getResource(UIViewType.INDEX.getViewName()));
        nodeObservableList.add(fxmlLoader.load()) ;
    }

    public static void switchToOrderList() throws IOException {
        nodeObservableList.clear();
        var fxmlLoader = new FXMLLoader(UIManager.class.getResource(UIViewType.ORDER.getViewName()));
        nodeObservableList.add(fxmlLoader.load()) ;
    }

    public static void switchToMemberList() throws IOException {
        nodeObservableList.clear();
        var fxmlLoader = new FXMLLoader(UIManager.class.getResource(UIViewType.MEMBER.getViewName()));
        nodeObservableList.add(fxmlLoader.load()) ;
    }

    public static void switchToStore() throws IOException {
        nodeObservableList.clear();
        var fxmlLoader = new FXMLLoader(UIManager.class.getResource(UIViewType.STORE.getViewName()));
        nodeObservableList.add(fxmlLoader.load()) ;
    }

}
